package com.ifgi.sitcom.campusmapperNDrawer.controllers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.util.Log;

import com.gamificationlibrary.model.leaderboard.Leaderboard;
import com.gamificationlibrary.model.*;
import com.google.android.maps.GeoPoint;
import com.ifgi.sitcom.campusmapperNDrawer.gamedata.PlayerAdapter;
import com.ifgi.sitcom.campusmapperNDrawer.io.RDFReader;
import com.ifgi.sitcom.campusmapperNDrawer.io.TripleStoreQueries;


public class LeaderboardController {

	private static LeaderboardController instance=null;
	private ArrayList<Leaderboard> leaderboards;
	private int[] numDays={15,30,Score.ALLTIME};

    public static LeaderboardController getInstance() {
       if(instance==null)
    	instance=new LeaderboardController();
	return instance;
    }
    
	
	public ArrayList<Leaderboard> getLeaderboards()
	{
		return leaderboards;
	}
	
	public void createLeaderboards()
	{
		leaderboards=new ArrayList<Leaderboard>();
		Area a=new Area(Score.ALLCOUNTRY,null);
		
		
		leaderboards.add(new Leaderboard(15, a));
		leaderboards.add(new Leaderboard(30, a));
		leaderboards.add(new Leaderboard(Score.ALLTIME, a));
		
		leaderboards.add(new Leaderboard(Score.ALLTIME, CampusMapperApplication.getInstance().getArea(1)));
		leaderboards.add(new Leaderboard(Score.ALLTIME, CampusMapperApplication.getInstance().getArea(2)));
		
	}
	
	
	public void populateLeaderboards(ArrayList<Player> people)
	{
		leaderboards.get(0).populate(people);
		leaderboards.get(1).populate(people);
		leaderboards.get(2).populate(people);
		
		leaderboards.get(3).populate(people);
		leaderboards.get(4).populate(people);
	
	}
		
	
	public ArrayList<Player> readAllPlayers(String[] buildingsURI) {
		// TODO Auto-generated method stub
		RDFReader read=new RDFReader();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		String today=dateFormat.format(cal.getTime());
		cal.add(Calendar.DATE, -30);
		String thirtyDaysAgo=dateFormat.format(cal.getTime());
		Calendar cal2 = Calendar.getInstance();
		cal2.add(Calendar.DATE, -15);
		String fifteenDaysAgo=dateFormat.format(cal2.getTime());
		String[] daysAgo={fifteenDaysAgo,thirtyDaysAgo};
		return read.getAllPlayersScore(daysAgo,today,buildingsURI);
	}
	
	
	public String queryBuildingsURI(Area area)
	{
		org.osmdroid.util.GeoPoint p1=area.getGeometry().get(0);
		org.osmdroid.util.GeoPoint p2=area.getGeometry().get(2);
		String buildingWithin=" ?building omgeo:within("+p1.getLatitude()+" "+p1.getLongitude()+" "+p2.getLatitude()+" "+p2.getLongitude()+") . ";
		TripleStoreQueries triples=new TripleStoreQueries();
		String buildingsURI=triples.queryBuildingsURI(buildingWithin);
		
		return buildingsURI;
	}


	public int[] getNumDays() {
		return numDays;
	}


	public void setNumDays(int[] numDays) {
		this.numDays = numDays;
	}
	
}
